using CommunityToolkit.Maui.Views;
using demo001.Models;
using System.ComponentModel;
using System.Windows.Input;

namespace demo001
{
    public partial class VideoPlayerPage : ContentPage, INotifyPropertyChanged
    {
        private readonly VideoViewModel _video;
        private bool _isPlaying;
        private double _volume = 1.0;
        private bool _isFullScreen;
        private double _progress;

        public VideoViewModel Video => _video;

        public string VideoSource => _video.LocalFilePath;

        public string PlayPauseButtonText => _isPlaying ? "暂停" : "播放";

        public string FullScreenButtonText => _isFullScreen ? "退出全屏" : "全屏";

        public double Volume
        {
            get => _volume;
            set
            {
                if (_volume != value)
                {
                    _volume = value;
                    VideoPlayer.Volume = value;
                    OnPropertyChanged(nameof(Volume));
                }
            }
        }

        public double Progress
        {
            get => _progress;
            set
            {
                if (_progress != value)
                {
                    _progress = value;
                    // 设置视频进度
                    if (VideoPlayer.Duration.TotalSeconds > 0)
                    {
                        VideoPlayer.SeekTo(TimeSpan.FromSeconds(VideoPlayer.Duration.TotalSeconds * value));
                    }
                    OnPropertyChanged(nameof(Progress));
                }
            }
        }

        public ICommand PlayPauseCommand { get; }
        public ICommand ToggleFullScreenCommand { get; }

        public VideoPlayerPage(VideoViewModel video)
        {
            InitializeComponent();
            _video = video;
            BindingContext = this;

            // 初始化命令
            PlayPauseCommand = new Command(TogglePlayPause);
            ToggleFullScreenCommand = new Command(ToggleFullScreen);

            // 绑定视频事件
            VideoPlayer.MediaOpened += OnMediaOpened;
            VideoPlayer.PositionChanged += OnPositionChanged;
        }

        private void OnMediaOpened(object sender, EventArgs e)
        {
            // 自动开始播放
            if (VideoPlayer != null)
            {
                
                Dispatcher.Dispatch(() =>
                {
                    VideoPlayer.Play();
                });
                _isPlaying = true;
                OnPropertyChanged(nameof(PlayPauseButtonText));
            }
        }

        private void OnPositionChanged(object sender, EventArgs e)
        {
            // 更新进度条
            if (VideoPlayer != null && VideoPlayer.Duration.TotalSeconds > 0)
            {
                _progress = VideoPlayer.Position.TotalSeconds / VideoPlayer.Duration.TotalSeconds;
                OnPropertyChanged(nameof(Progress));
            }
        }

        private void TogglePlayPause()
        {
            if (VideoPlayer != null)
            {
                if (_isPlaying)
                {
                    VideoPlayer.Pause();
                    _isPlaying = false;
                }
                else
                {
                    VideoPlayer.Play();
                    _isPlaying = true;
                }
                OnPropertyChanged(nameof(PlayPauseButtonText));
            }
        }

        private void ToggleFullScreen()
        {
            _isFullScreen = !_isFullScreen;
            OnPropertyChanged(nameof(FullScreenButtonText));

            // TODO: 实现真正的全屏逻辑
            // 这可能需要平台特定的实现
            DisplayAlert("提示", _isFullScreen ? "进入全屏" : "退出全屏", "确定");
        }

        // INotifyPropertyChanged 实现
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}